home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / pctim12.zip / MSCINT8.C < prev    next >
C/C++ Source or Header  |  1995-01-29  |  4KB  |  170 lines

  1. #include <dos.h>
  2. #include <conio.h>
  3. #include "mscint8.h"
  4.  
  5. #define IRQ0 0x8
  6. #define PIT0 0x40
  7. #define PIT1 0x41
  8. #define PIT2 0x42
  9. #define PITMODE 0x43
  10. #define PITCONST 1193180L
  11. #define PIT0DEF 18.2067597
  12. #define KBCTRL 0x61
  13. #define NEW8H 1
  14.  
  15. static float    tick_per_ms = 0.0182068;
  16. static float    ms_per_tick = 54.9246551;
  17. static float    freq8h = 18.2067597;
  18. static unsigned char flag8h = 0;
  19.  
  20. volatile int    counter_8h;
  21. volatile int    counter_reset;
  22. volatile unsigned long int ticks_8h;
  23.  
  24. void            init8h(unsigned int Hz)
  25. {
  26.     unsigned int    pit0_set,
  27.                     pit0_value;
  28.  
  29.     if (flag8h != NEW8H) {
  30.         old8h = _dos_getvect(IRQ0);
  31.         _dos_setvect(IRQ0, new8h);
  32.  
  33.         outp(PITMODE, 0x36);
  34.         pit0_value = PITCONST / Hz;
  35.         pit0_set = (pit0_value & 0x00ff);
  36.         outp(PIT0, pit0_set);
  37.         pit0_set = (pit0_value >> 8);
  38.         outp(PIT0, pit0_set);
  39.  
  40.         flag8h = NEW8H;
  41.         freq8h = Hz;
  42.         counter_8h = 0;
  43.         counter_reset = freq8h / PIT0DEF;
  44.         tick_per_ms = freq8h / 1000;
  45.         ms_per_tick = 1000 / freq8h;
  46.     }
  47. }
  48.  
  49. void            quit8h(void)
  50. {
  51.     unsigned int    pit0_set,
  52.                     pit0_value;
  53.     unsigned long   tick;
  54.     char           *cmostime;
  55.  
  56.     if (flag8h == NEW8H) {
  57.         outp(PITMODE, 0x36);
  58.         outp(PIT0, 0x00);
  59.         outp(PIT0, 0x00);
  60.         _dos_setvect(IRQ0, old8h);
  61.  
  62.         cmostime = get_cmostime();
  63.         tick = PIT0DEF *
  64.             (
  65.              (((float) *cmostime) * 3600) +
  66.              (((float) *(cmostime + 1)) * 60) +
  67.              (((float) *(cmostime + 2)))
  68.             );
  69.         _bios_timeofday(_TIME_SETCLOCK, &tick);
  70.  
  71.         flag8h = 0;
  72.         freq8h = PIT0DEF;
  73.         counter_reset = freq8h / PIT0DEF;
  74.         tick_per_ms = freq8h / 1000;
  75.         ms_per_tick = 1000 / freq8h;
  76.     }
  77. }
  78.  
  79. void _interrupt _far new8h(void)
  80. {
  81.     ticks_8h++;
  82.     counter_8h++;
  83.  
  84.     if (counter_8h == counter_reset) {
  85.         counter_8h = 0;
  86.         _chain_intr(old8h);
  87.     } else {
  88.         _disable();
  89.         outp(0x20, 0x20);
  90.     }
  91. }
  92.  
  93. unsigned long   time8h(unsigned long start, unsigned long stop)
  94. {
  95.     unsigned long   duration,
  96.                     millisec;
  97.  
  98.     if (stop < start)
  99.         return 0;
  100.     else {
  101.         duration = stop - start;
  102.         millisec = duration * ms_per_tick;
  103.         return millisec;
  104.     }
  105. }
  106.  
  107. void            delay8h(unsigned int delayms)
  108. {
  109.     unsigned long int delaybegin = 0;
  110.     unsigned long int delayend = 0;
  111.     unsigned int    delaytick;
  112.  
  113.     delaytick = delayms * tick_per_ms;
  114.  
  115.     if (flag8h == NEW8H)
  116.         delaybegin = ticks_8h;
  117.     else
  118.         _bios_timeofday(_TIME_GETCLOCK, &delaybegin);
  119.  
  120.     do {
  121.         if (flag8h == NEW8H)
  122.             delayend = ticks_8h;
  123.         else
  124.             _bios_timeofday(_TIME_GETCLOCK, &delayend);
  125.     } while ((delayend - delaybegin) < delaytick);
  126. }
  127.  
  128. void            sound8h(int freq, int duration)
  129. {
  130.     int             byte;
  131.     unsigned int    freq1;
  132.  
  133.     freq1 = PITCONST / freq;
  134.     outp(PITMODE, 0xb6);
  135.     byte = (freq1 & 0xff);
  136.     outp(PIT2, byte);
  137.     byte = (freq1 >> 8);
  138.     outp(PIT2, byte);
  139.     byte = inp(KBCTRL);
  140.     outp(KBCTRL, (byte | 3));
  141.  
  142.     delay8h(duration);
  143.     outp(KBCTRL, (byte & 0xfc));
  144. }
  145.  
  146. char           *get_cmostime(void)
  147. {
  148.     union REGS      inreg;
  149.     union REGS      outreg;
  150.     char           *buff;
  151.     static char     buffer[6];
  152.     char            ch;
  153.  
  154.     buff = buffer;
  155.     inreg.h.ah = 0x02;
  156.     int86(0x1a, &inreg, &outreg);
  157.  
  158.     ch = outreg.h.ch;
  159.     buffer[0] = (char) ((int) (ch & 0x0f) + (int) ((ch >> 4) & 0x0f) * 10);
  160.     ch = outreg.h.cl;
  161.     buffer[1] = (char) ((int) (ch & 0x0f) + (int) ((ch >> 4) & 0x0f) * 10);
  162.     ch = outreg.h.dh;
  163.     buffer[2] = (char) ((int) (ch & 0x0f) + (int) ((ch >> 4) & 0x0f) * 10);
  164.     buffer[3] = outreg.h.dl;
  165.     buffer[4] = (char) (outreg.x.cflag & 0x0001);
  166.     buffer[5] = 0x00;
  167.  
  168.     return (buff);
  169. }
  170.